package org.example.traffic.studentAccount;

import org.example.traffic.result.Result;
import org.example.traffic.student.Student;
import org.example.traffic.student.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/studentAccount")
public class StudentAccountController {
    @Autowired
    private StudentAccountRepository studentAccountRepository;

    @Autowired
    private StudentRepository studentRepository;

    private final StudentAccountService studentAccountService;

    public StudentAccountController(StudentAccountService studentAccountService) {
        this.studentAccountService = studentAccountService;
    }

    @PostMapping("/add")
    public Result add(@RequestBody StudentAccount studentAccount) {
        try {
            studentAccount = studentAccountService.getStudentAccount(studentAccount);
            System.out.println(studentAccount);
            studentAccountRepository.save(studentAccount);
            return Result.success();
        }catch (Exception e) {
            return Result.error();
        }
    }
}
